import random
import datetime
from django.db import models
from django.db.models import F


class PopularImg(models.Model):
    """
    推广图
    """
    name = models.CharField(verbose_name="名称", max_length=16)
    img = models.ImageField(verbose_name="推广图", help_text="jpg", upload_to="popular/index/")
    group = models.IntegerField(verbose_name="成组", help_text="需要成组设置成同样的数字")
    status = models.BooleanField(verbose_name="状态", default=True)

    class Meta:
        verbose_name = "推广图"
        verbose_name_plural = verbose_name
        ordering = ["group"]


class PasteInfo(models.Model):
    popular_img = models.ForeignKey("PopularImg", verbose_name="推广图")
    position = models.CharField(verbose_name="位置", help_text="(0,0)", max_length=32)
    paste_size = models.CharField(verbose_name="粘贴图片尺寸", help_text="(1,1)", max_length=32)

    class Meta:
        verbose_name = "粘贴图大小及位置"


class PasteInfoWords(models.Model):
    WORDS_TYPE = ((1, "手机号"), (2, "日期"))
    words_type = models.IntegerField(choices=WORDS_TYPE, default=1)
    popular_img = models.ForeignKey("PopularImg", verbose_name="推广图")
    position = models.CharField(verbose_name="位置", help_text="(0,0)", max_length=32)
    font_size = models.IntegerField(verbose_name="文字大小")
    font_file = models.FileField(verbose_name="字体", blank=True, null=True, upload_to="font_file")
    color = models.CharField(verbose_name="颜色", help_text="rgb,格式(0,0,0)", max_length=32, default="(255, 255, 255)")

    class Meta:
        verbose_name = "写入文字"


class SalesManImg(models.Model):
    """
    合成好的推广图
    """
    popular_img = models.ForeignKey("PopularImg", verbose_name="推广图")
    salesman = models.ForeignKey("sales_man.SalesMan", verbose_name="业务员")
    sales_img = models.CharField(max_length=64, verbose_name="推广图地址")
    create_time = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "业务员销售图"
        verbose_name_plural = verbose_name


class PopularCash(models.Model):
    cash = models.IntegerField(verbose_name="已获得")
    update_num = models.IntegerField(verbose_name="更新次数(每天)", default=0)
    update_time = models.DateTimeField(verbose_name="更新时间", auto_now=True)

    class Meta:
        verbose_name = "推广图里的已获得xx元"
        verbose_name_plural = verbose_name

    def update_cash(self):
        now_time = datetime.datetime.now()
        random_cash = random.randint(40000, 50000)
        if now_time.day == self.update_time.day and now_time.year == self.update_time.year and now_time.month == self.update_time.month:
            if now_time.hour > 10 and self.update_num == 0:
                # 第一次更新
                self.cash = F("cash") + random_cash
                self.update_num = F("update_num") + 1
                self.save()
            if now_time.hour > 15 and self.update_num == 1:
                self.cash = F("cash") + random_cash
                self.update_num = F("update_num") + 1
                self.save()
        else:
            self.update_num = 1
            self.cash = F("cash") + random_cash
            self.save()


class SalesManPopular(models.Model):
    """
    合成好的推广图（我的里）
    """
    popular_img = models.ForeignKey("sales_man.ChannelPopular", verbose_name="推广图")
    salesman = models.ForeignKey("sales_man.SalesMan", verbose_name="业务员")
    sales_img = models.CharField(max_length=64, verbose_name="推广图地址")
    create_time = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "业务员推广图(我的)"
        verbose_name_plural = verbose_name
